package com.saxon.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class loginFilter implements Filter {
    @Override
    public void init (FilterConfig filterConfig) throws ServletException {

    }

    //登陆验证拦截器
    @Override
    public void doFilter (ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        HttpServletResponse httpServletResponse = (HttpServletResponse) response;
        if (httpServletRequest.getRequestURI ().contains ("register")) {
            httpServletRequest.getRequestDispatcher ("/JSP/register.jsp").forward (httpServletRequest, httpServletResponse);
        }
        if (httpServletRequest.getRequestURI ().contains ("update")) {
            httpServletRequest.getRequestDispatcher ("/JSP/updatePassword.jsp").forward (httpServletRequest, httpServletResponse);
        }
        if (httpServletRequest.getSession ().getAttribute ("user") == null) {
            httpServletRequest.getRequestDispatcher ("/JSP/index.jsp").forward (httpServletRequest, httpServletResponse);
        }
        chain.doFilter (request, response);
    }

    @Override
    public void destroy () {

    }
}
